package com.inter.insideinterservice.newservice;

import com.inter.entity.common.DeviceFlowUsage;
import com.inter.entity.newpojo.Constant;
import com.inter.entity.newpojo.TurntoInterfaceUtils;
import com.inter.entity.newpojo.request.RequestBody.QuerySpecifiedDeviceAndMonthRequestBody;
import com.inter.entity.newpojo.request.RequestHeader;
import com.inter.entity.newpojo.request.subRequest.QuerySpecifiedDeviceAndMonthRequest;
import com.inter.entity.newpojo.response.subResponse.QuerySpecifiedDeviceAndMonthResponse;
import com.inter.utils.HttpClientTool;
import com.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

public class TurnToQueryDeviceFlowUsageService {

    private static Logger logger = Logger.getLogger(TurnToQueryDeviceFlowUsageService.class);

    //1，指定了设备，指定了月份，查询流量的适用情况
    public DeviceFlowUsage querySpecifiedDeviceAndMonth(String deviceId, String month, String partnerCode, String secret) {
        try {
            QuerySpecifiedDeviceAndMonthRequestBody body =new QuerySpecifiedDeviceAndMonthRequestBody();
            body.setDeviceId(deviceId);
            body.setMonth(month);

            // 请求参数拼接
            QuerySpecifiedDeviceAndMonthRequest request = new QuerySpecifiedDeviceAndMonthRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_singleDeviceMonthFlow, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            //logger.info("签名是：" + sign);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(QuerySpecifiedDeviceAndMonthRequest.class).toXml(request, "utf-8");
            //logger.info("请求内容：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            //logger.info("返回结果：" + result);

            QuerySpecifiedDeviceAndMonthResponse responseObj = new JaxbUtil(QuerySpecifiedDeviceAndMonthResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode) && responseObj.getBody().getFlow()!=null) {
                //成功，拿到回传的结果回去处理
                DeviceFlowUsage flowUsage=new DeviceFlowUsage();
                flowUsage.setFlow(responseObj.getBody().getFlow());
                flowUsage.setRetCode(retCode);
                flowUsage.setResultInfo(resultInfo);
                return flowUsage;
            } else {
                DeviceFlowUsage flowUsage=new DeviceFlowUsage();
                flowUsage.setRetCode(retCode);
                flowUsage.setResultInfo(resultInfo);
                return flowUsage;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }



}
